AddCSLuaFile()

if SERVER then
	AddCSLuaFile("shared.lua")
else
   ENT.PrintName = "Bear"
end
 
ENT.AutomaticFrameAdvance = true
 
ENT.Type = "anim"
ENT.Model = Model("models/animals/bear_model.mdl")
ENT.Name = "Glinting Stone"
ENT.Follow = nil

ENT.RunThreshold = 180
ENT.WalkThreshold = 120

ENT.OldSequence = "idle"
ENT.CurSequence = "idle"

ENT.SoundCooldown = CurTime()

function ENT:Initialize()
	self.Entity:SetModel(self.Model)
	self.Entity:SetModelScale( self.Entity:GetModelScale() * 1.1, 0 )
	self.Entity:SetPlaybackRate( 1 )
	self.Entity:ResetSequence(self.CurSequence)
	
	self.bearmodel = ents.Create("prop_dynamic")
	--self.bearmodel:SetParent(self.Entity:GetParent())
	self.bearmodel:SetPos(self.Entity:GetPos() + Vector(0, 0, -38))
	self.bearmodel:SetAngles(self.Entity:GetAngles())
	self.bearmodel:SetLocalAngles(Angle(0, 0, 0))
	self.bearmodel:SetModel(self.Model)
	self.bearmodel:SetPlaybackRate( 1 )
	self.bearmodel:Spawn()
	self.bearmodel:Activate()
	self.bearmodel:SetPredictable(true)
	self.bearmodel:SetModelScale( 1.1, 0 );
end

function ENT:Think()
	self:AnimationSounds()
	print(self.Follow)
	if(self.Follow)then
		--print("Saggy meat-curtains.")
		local yaw = 0
		local angles = Angle(0, 0, 0)
		yaw = self.Follow:GetAngles().y
		
		if(self.Follow and self.Follow:GetVelocity():Length() > 0 and self.Follow:OnGround())then
			yaw = self.Follow:GetVelocity():Angle().y
		end
		
		angles = Angle(self.bearmodel:GetAngles().p, yaw, self.bearmodel:GetAngles().r)
		self.bearmodel:SetAngles(angles)
		self.bearmodel:SetPos(self.Follow:GetPos())
		
		local velocity = self.Follow:GetVelocity()
		local speed = velocity:Length()
		local frame = self.bearmodel:GetCycle()
		if(!self.Attacking)then
			if(speed > 0)then
				if(speed < self.WalkThreshold)then
					self.CurSequence = "walk"
					self.bearmodel:SetPlaybackRate( 2 )
				elseif(speed > self.WalkThreshold)then
					self.CurSequence = "run"
					self.bearmodel:SetPlaybackRate( 0.5 * (speed/self.RunThreshold))
				end
			elseif(!self.Attacking)then
				self.CurSequence = "idle"
			end
		end
		if(self.CurSequence != self.OldSequence)then
			self.OldSequence = self.CurSequence
			self.bearmodel:ResetSequence(self.CurSequence)
		end
	end
	self:NextThink(CurTime())
	return true
end

function ENT:AnimationSounds()
	local cycle = self.Entity:GetCycle()
	local frame = 0
	if(CurTime() > self.SoundCooldown)then
		if(self.CurSequence == "run")then
			frame = math.floor(cycle * 15)
			if(frame == 0)then
				self.Entity:EmitSound("npc/zombie/foot"..math.random(2,3)..".wav", 70, math.random(20,40))
				self.SoundCooldown =  CurTime() + 0.2;
			elseif(frame == 3)then
				self.Entity:EmitSound("npc/zombie/foot"..math.random(2,3)..".wav", 80, math.random(30,50))
				self.SoundCooldown =  CurTime() + 0.2;
			elseif(frame == 12)then
				self.Entity:EmitSound("npc/zombie/foot"..math.random(2,3)..".wav", 70, math.random(20,40))
				self.SoundCooldown =  CurTime() + 0.2;
			end
		end
	end
end






